package com.hellobike.haxing.modules.supplier.task;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.hellobike.haxing.modules.purchase.mapper.TPurchaseProMapper;
import com.hellobike.haxing.modules.supplier.dto.ProductSoldDto;
import com.hellobike.haxing.modules.wms.dto.ExportShippingShopPdfDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class ProductSalesMailTask {
    @Autowired
    private TPurchaseProMapper proMapper;

    @Autowired
    private TemplateEngine templateEngine;

    @Autowired
    private JavaMailSender javaMailSender;


    @Scheduled(cron = "0 0 8,10,12,14,16,18,20,21,22 * * ?")
    public void sendSoldInfo() {
        Context context = new Context();  //thymeleaf包下的
        List<ProductSoldDto> list = proMapper.selectProductSold();
        Map<String, Object> map = new HashMap<>();
        map.put("productSoldList", list);
        context.setVariables(map);
        String html = templateEngine.process("product_sold", context);
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setSentDate(new Date());
            helper.setFrom("83066420@qq.com");
            helper.setTo(new String[]{"gengmengmeng07938@hellobike.com",
                    "qiaobeibei11501@hellobike.com",
                    "wangsiyu11680@hellobike.com",
                    "yanyongjian795@hellobike.com",
                    "shenjiayou11157@hellobike.com","13585175397@139.com","fangjiwei800@hellobike.com"});
            helper.setSubject("商品销量");
            helper.setText(html, true);
            javaMailSender.send(mimeMessage);
        } catch (Exception ex) {
            log.error("", ex);
        }

    }
}
